<?php
session_start();
//INSTALL CLIENT
if (isset($_POST['install']) and  $_POST['install']=="ok"){
	
}
function create_base($link){					
	if (mysql_query("CREATE DATABASE ".$_SESSION['database_name'].";" , $link))
		{
	  		return true;
	  	}
	else
	  	{
	  		return false;
	  	}
}
function create_table($database){
		mysql_select_db($database);	  	
		$request = "CREATE TABLE IF NOT EXISTS `search_folder` ( `id` int(20) NOT NULL auto_increment, `name` varchar(255) NOT NULL COMMENT 'le nom du partage', `chemin` varchar(255) NOT NULL COMMENT 'le chemin',  `type` varchar(30) default NULL COMMENT 'type of file', PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table de search' AUTO_INCREMENT=1 ;";
		mysql_query($request) or die(mysql_error());
		$request = "CREATE TABLE IF NOT EXISTS `search_server` ( `id` int(20) NOT NULL auto_increment, `name` varchar(255) NOT NULL COMMENT 'nom du server',`address` varchar(255) NOT NULL COMMENT 'adresse du server',`pin` varchar(255) NOT NULL COMMENT 'code pin avec le server', PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table des server' AUTO_INCREMENT=3 ;";						
		mysql_query($request) or die(mysql_error());
		$request = "CREATE TABLE IF NOT EXISTS `search_id` (  `id` int(255) NOT NULL auto_increment COMMENT 'id de l''id generer',  `id_generate` varchar(255) NOT NULL COMMENT 'id genere',  `film` varchar(255) NOT NULL COMMENT 'nom du film',  `date` varchar(255) NOT NULL COMMENT 'date de la demande',  `user` varchar(255) NOT NULL COMMENT 'nom du user',  `server` varchar(255) NOT NULL COMMENT 'nom du server',  `valid` int(1) NOT NULL, PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table de l''id' AUTO_INCREMENT=1 ;";
		mysql_query($request) or die(mysql_error());
		//$request = "CREATE TABLE IF NOT EXISTS `search_log` (  `id` int(255) NOT NULL auto_increment,  `action` varchar(255) default NULL,  `server` varchar(255) default NULL,  `user` varchar(255) default NULL,  `date` varchar(255) default NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
		//mysql_query($request) or die(mysql_error());
		$request = "CREATE TABLE IF NOT EXISTS `search_log` (  `id` int(255) NOT NULL auto_increment,  `action` varchar(255) default NULL,  `server` varchar(255) default NULL,  `user` varchar(255) default NULL,  `date` varchar(255) default NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
		mysql_query($request) or die(mysql_error());
		$request = "CREATE TABLE IF NOT EXISTS `search_user` (  `id` int(5) NOT NULL auto_increment,  `name` varchar(10) NOT NULL,  `md5` varchar(50) NOT NULL,  `last_connect` varchar(50) NOT NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
		mysql_query($request) or die(mysql_error());
		
}
$etape=0;
//premiere étape
if (isset($_POST['install']) and $_POST['install']=="ok"){
	if (isset($_POST['database_name']) and $_POST['database_name']!=""){
		if (isset($_POST['database_address']) and $_POST['database_address']!=""){
			if (isset($_POST['database_user']) and $_POST['database_user']!=""){
				if (isset($_POST['database_password']) and $_POST['database_password']!=""){
					$_SESSION['database_password']=$_POST['database_password'];
					$_SESSION['database_user']=$_POST['database_user'];
					$_SESSION['database_address']=$_POST['database_address'];
					$_SESSION['database_name']=$_POST['database_name'];
					
					$link = @mysql_connect($_SESSION['database_address'], $_SESSION['database_user'], $_SESSION['database_password']);
					if ($link){
						if ($_POST['database_create'][0] == "true"){	
							if (create_base($link)){
								$message="database create OK";
							}
							else{
								$error="database create Failed";
							}
						}
						create_table($_POST['database_name']);
						mysql_close($link);
						$etape=1;
					}
					else{
						$error="user, address or password refused";
					}
				}
				else{
					$error="Database password is empty";
				}
			}
			else{
				$error="Database user is empty";
			}
		}
		else{
			$error="Database address is empty";
		}
	}
	else{
		$error="Database name is empty";
	}
}

//deuxieme etape
if (isset($_GET['etape']) and $_GET['etape']=="user"){
	$etape=2;
}
//troisieme etape
if (isset($_POST['install']) and $_POST['install']=="config"){
	if (isset($_POST['user_name']) and $_POST['user_name']!=""){
		if (isset($_POST['user_password']) and $_POST['user_password']!=""){
			$link = @mysql_connect($_SESSION['database_address'], $_SESSION['database_user'], $_SESSION['database_password']);
			mysql_select_db($_SESSION['database_name']);
			if (file_exists("config.php")){
				unlink("config.php");
			}
			//generation de config.php
			$e=fopen("config.php","a+");
			if (! $e){
				print "";
				print "Fichier pas crée (check if the folder is in chmod 777)";
				exit();
			}
			else{
				$request = "INSERT INTO `search_user` (`name`, `md5`, `last_connect`) VALUES('".$_POST['user_name']."', '".md5($_POST['user_password'])."', '".time()."');";
				mysql_query($request) or die(mysql_error());
				fputs($e,"<?php\n");
				fputs($e,"//config CLIENT\n");
				fputs($e,"	//this file is the config file you must to set the correct variable\n");
				//fputs($e,"	\$admin_login='".$_POST['user_name']."';\n");
				//fputs($e,"	\$admin_passwd='".$_POST['user_password']."';");
				fputs($e,"	//this part for the database config\n");
				fputs($e,"	\$database_address='".$_SESSION['database_address']."';\n");
				fputs($e,"	\$database_name='".$_SESSION['database_name']."';\n");
				fputs($e,"	\$database_user='".$_SESSION['database_user']."';\n");
				fputs($e,"	\$database_pass='".$_SESSION['database_password']."';\n");
				fputs($e,"	\$database_table_folder='search_folder';\n");
				fputs($e,"	\$database_table_server='search_server';\n");
				fputs($e,"	\$database_table_id='search_id';\n");
				fputs($e,"	\$database_table_log='search_log';\n");
				fputs($e,"	\$database_table_user='search_user';\n");
				fputs($e,"	");
			
				fputs($e,"	\$typefiles[0]='Movies';\n");
				fputs($e,"	\$typefiles[1]='Series';\n");
				fputs($e,"	\$typefiles[2]='Other';\n");
			
				
				fputs($e,"	");
				fputs($e,"	//config affichage web\n");
				fputs($e,"	\$html_title='Search_podzone';\n");
				fputs($e,"	");
				fputs($e,"	//don't touch or DIE\n");
				fputs($e,"	define('DATABASE_ADDRESS',\$database_address);\n");
				fputs($e,"	define('DATABASE_NAME',\$database_name);\n");
				fputs($e,"	define('DATABASE_USER',\$database_user);\n");
				fputs($e,"	define('DATABASE_PASS',\$database_pass);\n");
				fputs($e,"	define('DATABASE_TABLE_FOLDER',\$database_table_folder);\n");
				fputs($e,"	define('DATABASE_TABLE_SERVER',\$database_table_server);\n");
				fputs($e,"	define('DATABASE_TABLE_ID',\$database_table_id);\n");
				fputs($e,"	define('DATABASE_TABLE_LOG',\$database_table_log);\n");
				fputs($e,"	define('DATABASE_TABLE_USER',\$database_table_user);\n");
				fputs($e,"	define('ADMIN_LOGIN',\$admin_login);\n");
				fputs($e,"	define('ADMIN_PASS',\$admin_passwd);\n");
				fputs($e,"	\n");
				fclose($e);
				if (file_exists("config.php")){
						$message="Fichier Crée";
						$etape=3;
				}
				else{
					$error="Fichier pas crée (check if the folder is in chmod 777)";	
				}
			}
		}
		else{
			$error="Need password";
		}
	}
	else{
		$error="Need username";
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
	<title>Install of Search</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="total3">	
	<div id="error">
	<?php
		//affichage des erreurs
		if (isset($error) and $error != ""){
			echo "ERROR : ".$error;
			$error = "";
		}
	?>
	</div>
	<div id="msg">
	<?php	if (isset($message) and $message != ""){
			echo "message : ".$message;
			$message = "";
		}
	?>
	</div>
	<div id="form">
	<?php 
	if($etape==0){
	?>

	<form action="install.php" method="post">
	<?php 
	session_destroy();
	?>
	<table>
	<tr>
		<td>Nom de la base</td><td><input type="text" name="database_name" value="<?php print $_SESSION['database_name']; ?>" /></td>
	</tr>
	<tr>
		<td></td><td>Create base <input type="checkbox" name="database_create[]" value="true" /></td>
	</tr>
	<tr>
		<td>Adresse de la base</td><td><input type="text" name="database_address" value="<?php print $_SESSION['database_address']; ?>" /></td>
	</tr>
	<tr>
		<td>Nom de l'utilisateur</td><td><input type="text" name="database_user" value="<?php print $_SESSION['database_user']; ?>" /></td>
	</tr>
	<tr>
		<td>Mot de passe de l'utilisateur</td><td><input type="password" name="database_password" value="<?php print $_SESSION['database_password']; ?>" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="hidden" name="install" value="ok" /><input type="submit" value="install"  /></td>
	</tr>
	</table>
	</form>
	<?php
	 } 
	if ($etape==1){
		print "<p>Install Database OK</p>";
		print "<a href=\"install.php?etape=user\">Next Step</a>";
	}
	if ($etape==2){
	?>
	<form action="install.php" method="post">
	<table>
	<tr>
		<td>Nom de l'utilisateur de Search</td><td><input type="text" name="user_name" value="admin" /></td>
	</tr>
	<tr>
		<td>password</td><td><input type="password" name="user_password" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="hidden" name="install" value="config" /><input type="submit" value="install"  /></td>
	</tr>
	</table>
	</form>	
		
	<?php
	}
	if ($etape==3){
		print "<p>INSTALL Finished</p>";
		print "<p>Please remove install.php</p>";
	}
	?>
	</div>
</div>
</body>
</html>